﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using PracticeSimple.Models;
using BusinessObjects;

namespace PracticeSimple.Controllers
{
    public class DashboardController : SecurityController
    {

        public ActionResult Index()
        {
            DashboardModel model = new DashboardModel();

            model.Facilities = DataLayer.GetRepo.Facility().GetFacilitiesList();

            return View(model);
        }


        public JsonResult FindPatients(int facilityID, string searchText)
        {
            List<Patient> patients = null;

            if (searchText.Trim().Contains(" "))
            {
                string firstName = searchText.Split(' ')[0];
                string lastName = searchText.Split(' ')[0];
                patients = DataLayer.GetRepo.Patient().FindPatients(facilityID, firstName, lastName);
            }
            else
            {
                patients = DataLayer.GetRepo.Patient().FindPatients(facilityID, searchText);

                if (patients.Count < 1)
                {
                    patients = DataLayer.GetRepo.Patient().FindPatientsLike(facilityID, searchText);
                }
            }      

            return Json(patients, JsonRequestBehavior.AllowGet);
        }

    }
}
